package net.lab1024.sa.admin.module.business.category.task;

import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.category.constant.CategoryTypeEnum;
import net.lab1024.sa.admin.module.business.category.domain.form.CategoryAddForm;
import net.lab1024.sa.admin.module.business.category.service.CategoryService;
import net.lab1024.sa.common.common.domain.ResponseDTO;
import net.lab1024.sa.common.module.support.jdunion.JdUnionService;
import net.lab1024.sa.common.module.support.jdunion.domain.vo.GoodsCategoryVO;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class SyncGoodsCategoryTask
{

    @Autowired
    private JdUnionService jdUnionService;

    @Autowired
    private CategoryService categoryService;


//    @Scheduled(fixedRate = 3600000)
    public void run() {
        ResponseDTO<List<GoodsCategoryVO>> goodsCategoryRsp = jdUnionService.getGoodsCategory(0, 0);


        if(goodsCategoryRsp.getCode().equals(ResponseDTO.OK_CODE)){

            List<GoodsCategoryVO> goodsCategoryList1 = goodsCategoryRsp.getData();
            for(GoodsCategoryVO goodsCategoryVO1:goodsCategoryList1){
                CategoryAddForm categoryAddForm1=new CategoryAddForm();
                categoryAddForm1.setCategoryName(goodsCategoryVO1.getName());
                categoryAddForm1.setCategoryType(CategoryTypeEnum.GOODS.getValue());
                categoryAddForm1.setParentId(NumberUtils.LONG_ZERO);
                categoryAddForm1.setOtherId(goodsCategoryVO1.getId().longValue());

                Long categoryId1=NumberUtils.LONG_ZERO;


                ResponseDTO<String> queryCategoryIdRsp1 = categoryService.queryCategoryId(NumberUtils.LONG_ZERO, goodsCategoryVO1.getName(), CategoryTypeEnum.GOODS.getValue());

                if(queryCategoryIdRsp1.getCode().equals(ResponseDTO.OK_CODE)){

                    categoryId1=Long.parseLong(queryCategoryIdRsp1.getData());

                }else{
                    ResponseDTO<String> addRsp1 = categoryService.add(categoryAddForm1);


                    if(addRsp1.getCode().equals(ResponseDTO.OK_CODE)){

                        categoryId1=Long.parseLong(addRsp1.getData());
                    }else{
                        log.error("一级分类添加失败：{}",goodsCategoryVO1.getName());
                        continue;
                    }

                }





                goodsCategoryRsp=jdUnionService.getGoodsCategory(1,goodsCategoryVO1.getId());

                List<GoodsCategoryVO> goodsCategoryList2 = goodsCategoryRsp.getData();


                for(GoodsCategoryVO goodsCategoryVO2:goodsCategoryList2){
                    CategoryAddForm categoryAddForm2=new CategoryAddForm();
                    categoryAddForm2.setCategoryName(goodsCategoryVO2.getName());
                    categoryAddForm2.setCategoryType(CategoryTypeEnum.GOODS.getValue());
                    categoryAddForm2.setParentId(categoryId1);
                    categoryAddForm2.setOtherId(goodsCategoryVO2.getId().longValue());

                    ResponseDTO<String> queryCategoryIdRsp2 = categoryService.queryCategoryId(categoryId1, goodsCategoryVO2.getName(), CategoryTypeEnum.GOODS.getValue());

                    Long categoryId2;


                    if(queryCategoryIdRsp2.getCode().equals(ResponseDTO.OK_CODE)){

                        categoryId2=Long.parseLong(queryCategoryIdRsp2.getData());

                    }else{
                        ResponseDTO<String> addRsp2 = categoryService.add(categoryAddForm2);


                        if(addRsp2.getCode().equals(ResponseDTO.OK_CODE)){

                            categoryId2=Long.parseLong(addRsp2.getData());
                        }else{
                            log.error("二级分类添加失败：{}",goodsCategoryVO2.getName());
                            continue;
                        }

                    }

                    goodsCategoryRsp=jdUnionService.getGoodsCategory(2,goodsCategoryVO2.getId());

                    List<GoodsCategoryVO> goodsCategoryList3 = goodsCategoryRsp.getData();


                    for(GoodsCategoryVO goodsCategoryVO3:goodsCategoryList3){






                        CategoryAddForm categoryAddForm3=new CategoryAddForm();
                        categoryAddForm3.setCategoryName(goodsCategoryVO3.getName());
                        categoryAddForm3.setCategoryType(CategoryTypeEnum.GOODS.getValue());
                        categoryAddForm3.setParentId(categoryId2);
                        categoryAddForm3.setOtherId(goodsCategoryVO3.getId().longValue());

                        ResponseDTO<String> queryCategoryIdRsp3 = categoryService.queryCategoryId(categoryId2, goodsCategoryVO3.getName(), CategoryTypeEnum.GOODS.getValue());



                        if(!queryCategoryIdRsp3.getCode().equals(ResponseDTO.OK_CODE)){


                            ResponseDTO<String> addRsp3 =categoryService.add(categoryAddForm3);



                            if(!addRsp3.getCode().equals(ResponseDTO.OK_CODE)){

                                log.error("三级分类添加失败：{}",goodsCategoryVO3.getName());
                                continue;
                            }

                        }

                        categoryService.add(categoryAddForm3);

                    }


                }

            }
        }

        System.out.println("定时任务执行...");

    }
}
